package com.hustzp.com.xichuangzhu.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.model.Review;
import com.hustzp.com.xichuangzhu.poetry.QuotesAct;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

/**
 * Created by Administrator on 2016/6/5.
 */
public class AddQuoteActivity extends MyBaseActivity {

    private EditText quoteTitle;
    private EditText quoteAuthor;
    private Button addQuoteBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_quote);

        initToolbar();
        initViews();
    }

    private void initToolbar() {
        TextView navTxt = (TextView) findViewById(R.id.tv_nav_txt);
        navTxt.setText("摘录");

        TextView appTitle = (TextView) findViewById(R.id.tv_title_txt);
        appTitle.setText("自由创作");
    }

    private void initViews() {
        quoteTitle = (EditText) findViewById(R.id.tv_quote_title);
        quoteAuthor = (EditText) findViewById(R.id.tv_quote_author);

        // 推荐
        addQuoteBtn = (Button) findViewById(R.id.add_quote_btn);
        addQuoteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(quoteTitle.getText().toString().trim())) {
                    DialogFactory.hintDialog(AddQuoteActivity.this, "文字不可为空");
                    return;
                }

                if (TextUtils.isEmpty(quoteAuthor.getText().toString().trim())) {
                    DialogFactory.hintDialog(AddQuoteActivity.this, "作者不可为空");
                    return;
                }

                AVObject quoteObj = new AVObject("CustomQuote");
                quoteObj.put("user", AVUser.getCurrentUser());
                quoteObj.put("quote", quoteTitle.getText().toString().trim());
                quoteObj.put("author", quoteAuthor.getText().toString().trim());
                quoteObj.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            Review review = new Review();
                            review.setAuthor(quoteAuthor.getText().toString().trim());
                            review.setQuote(quoteTitle.getText().toString().trim());
                            review.setQuote_tr(quoteTitle.getText().toString().trim());
                            Intent intent = new Intent(AddQuoteActivity.this, QuotesAct.class);
                            intent.putExtra("from", AddQuoteActivity.class.getSimpleName());
                            intent.putExtra(QuotesAct.class.getSimpleName(), review);
                            startActivity(intent);
                        } else {
                            DialogFactory.hintDialog(AddQuoteActivity.this, "自由创作保存失败，请重试");
                        }
                    }
                });
            }
        });
    }

    public void recommend(View view) {
        showToastInfo("推荐");
    }

}
